package com.cipe.cmrs.validator;

import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;

public abstract class AbstractValidator {
    protected Logger log = LoggerFactory.getLogger(getClass());
    private MessageSourceAccessor messageSourceAccessor;

    @Autowired
    public void setMessageSourceAccessor(MessageSourceAccessor messageSourceAccessor) {
        this.messageSourceAccessor = messageSourceAccessor;
    }

    public String getText(String msgKey) {
        return getText(msgKey, null);
    }

    /**
     * Convenience method for getting a i18n key's value. Calling
     * getMessageSourceAccessor() is used because the RequestContext variable is
     * not set in unit tests b/c there's no DispatchServlet Request.
     * 
     * @param msgKey
     * @param locale
     *            the current locale
     * @return
     */
    public String getText(String msgKey, Locale locale) {
        return messageSourceAccessor.getMessage(msgKey, locale);
    }

    /**
     * Convenient method for getting a i18n key's value with a single string
     * argument.
     * 
     * @param msgKey
     * @param arg
     * @param locale
     *            the current locale
     * @return
     */
    public String getText(String msgKey, String arg, Locale locale) {
        return getText(msgKey, new Object[]{ arg }, locale);
    }

    /**
     * Convenience method for getting a i18n key's value with arguments.
     * 
     * @param msgKey
     * @param args
     * @param locale
     *            the current locale
     * @return
     */
    public String getText(String msgKey, Object[] args, Locale locale) {
        return messageSourceAccessor.getMessage(msgKey, args, locale);
    }
}
